#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Git Log Helpers ===
alias gitlo='git log --pretty=oneline --abbrev-commit';
alias gitlod='git log --pretty=oneline --abbrev-commit --';
alias gitloo='git log --pretty=format:"%C(yellow)%h %Cred%ad %Creset%s" --date=short --all --';

# === Git Log Searcher ===
function gitlos()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: gitlos <path_or_.> ["search string input"] [search_max_count] (Search string in git history)';
    echo '';
    return;
  fi;

  # Variables
  local input;
  local path;
  local cnt;

  # Handle input values
  if [ -f "${1}" ] || [ -d "${1}" ]; then
    path=${1};
    input=${2};
    cnt=${3:-30};
  else
    path=.;
    input=${1};
    cnt=${2:-30};
  fi;

  # Search related commits
  echo '';
  echo -e " \e[1;37mSearching for ${input} in last ${cnt} commits...\e[0m";
  echo '';
  for sha1 in $(git log --max-count="${cnt}" --format=format:%H -- "${path}"); do
    if git show "${sha1}" | grep -iq "${input}"; then
      git --no-pager show -M --name-only --oneline "${sha1}";
      git --no-pager show -M "${sha1}" | grep -i "${input}";
      echo '';
    fi;
  done;
}

# === Git Diff Status ===
function gitdi()
{
  # Usage: gitdi (Show git differences status)

  # Show unmerged diffs
  git diff --name-status;
  git ls-files --others --exclude-standard | awk '{print "U       " $0}';
}

# === Git Diff Status SHA1 ===
function gitdfs()
{
  # Usage
  if [ -z "${2}" ]; then
    echo '';
    echo ' Usage: gitdfs <sha1commit> (git diff status of an SHA1)';
    echo '';
    return;
  fi;

  # Diff commits
  git diff-tree --no-commit-id --name-status -r "${1}";
}

# === Git Diff SHA1 On Path ===
function gitdf()
{
  # Usage
  if [ -z "${2}" ]; then
    echo '';
    echo ' Usage: gitdf <sha1commit> <filepath> (git diffs from an SHA1)';
    echo '';
    return;
  fi;

  # Diff commits with paths
  git diff "${1}^:${2}" "${1}:${2}";
}

# === Git Commit Finder ===
function gitfindsha1()
{
  # Usage
  if [ -z "${2}" ]; then
    echo '';
    echo ' Usage: gitfindsha1 <remote/branch> <"title text to search"> (git search commit by title contents)';
    echo '';
    return;
  fi;

  # Search
  git log --pretty=oneline "${1}" \
    | grep "${2}" \
    | head -n 1 \
    | cut -f 1 -d ' ';
}

# === Git Unfiltered Helper ===
function gitnofilter()
{
  # Usage: gitnofilter <parameters> (Use git commands without filters)

  # Use git commands without filters
  git -c core.fileMode=true -c core.autocrlf=false "${@}";
}
